/**
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
输入：l1 = [1,2,4], l2 = [1,3,4]
输出：[1,1,2,3,4,4]
 */

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
function mergeTwoLists(l1, l2) {
  let head = new ListNode(0)
  let res = head
  while(list1&&list2) {
    if(list1.val<=list2.val) {
      res.next = list1;
      list1 = list1.next
    }else {
      res.next = list2
      list2 = list2.next
    }
    res = res.next
  }
  res.next = list1 || list2
  return head.next
}
const l1 = [1,2,4];
const l2 = [1,3,4];
console.log(mergeTwoLists(l1, l2));
